<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Internship extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->headerdata = array();
		if (($this->session->userdata('userid')>0) && ($this->session->userdata('hash')==md5($this->session->userdata('userid').$this->session->userdata('username')))){
			$this->headerdata['userid'] = $this->session->userdata('userid');
			$this->headerdata['username'] = $this->session->userdata('username');
		}else{
			$this->headerdata['userid'] = 0;
			$this->headerdata['username'] = 'Guest';
		}
		
		$this->headerdata['route'] = 'localhost/source';
		$this->headerdata['routelink'] = '';
		
		if ($this->session->userdata('userid')==0) redirect(site_url());
		
		if (($this->session->userdata('msg') != '') && ($this->session->userdata('msgtype') != '')){
			$this->headerdata['msg'] = $this->session->userdata('msg');
			$this->headerdata['msgtype'] = $this->session->userdata('msgtype');
			$this->session->set_userdata('msg','');
			$this->session->set_userdata('msgtype','');
		}
		
		$q = $this->db->get('option');
		$option = array();
		
		$this->option = $q->result();
	}
	
	function index(){
		$data = array();
		$success = '';
		$error = '';
		
		$this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		$this->load->model('CompanyModel');
		$data['toTime'] = $this->CompanyModel->getStarttime();
		$data['endTime'] = $this->CompanyModel->getDeadline();
		
		$this->db->where('userid',$this->session->userdata('userid'));
		$q = $this->db->get('link');//select * from link
			
		$this->db->where('userid',$this->session->userdata('userid'));
		$q2 = $this->db->get('link_other');
		
		if ($q->num_rows() || $q2->num_rows()){
			$this->headerdata['route2'] = 'Kết quả đăng ký';

			$data['success'] = $success;
			$data['error'] = $error;
			
			if ($q2->num_rows()){
				$data['company_other'] = $q2->first_row();
			}
			if($q->num_rows()){
				$this->db->where('link.userid',$this->session->userdata('userid'));
				$this->db->join('company','company.id=link.companyid');
				$q = $this->db->get('link');
				$data['companies'] = $q->result();
			}

			$this->load->view('common/header',$this->headerdata);
			$this->load->view('common/left', $data);
			$this->load->view('internship_result',$data);
			$this->load->view('common/footer');
		}else{
			$this->headerdata['route2'] = 'Kết quả đăng ký';
			$data['companies'] = $this->CompanyModel->get();
			$data['selected'] = array();
			foreach ($q->result() as $l){
				$data['selected'][] = $l->companyid;
			}
			
			$this->load->model('UserModel');
			$data['user'] = $this->UserModel->get($this->headerdata['userid']);
			$data['success'] = $success;
			$data['error'] = $error;
			$this->load->view('common/header',$this->headerdata);
			$this->load->view('common/left', $data);
			$this->load->view('intership',$data);
			$this->load->view('common/footer');
		}
	}
	
	function signup(){
		$this->load->model('CompanyModel');
		$toTime = $this->CompanyModel->getStarttime();
		$endTime = $this->CompanyModel->getDeadline();
		
		if (($toTime <= time()) && ($endTime > time())){
			
			// Check if already sign up
			$this->db->where('userid',$this->session->userdata('userid'));
			$q = $this->db->get('link');
			
			$this->db->where('userid',$this->session->userdata('userid'));
			$q2 = $this->db->get('link_other');
			
			if ($q->num_rows() || $q2->num_rows()){
				redirect(site_url('internship'));
			}
			
			// Check sign-up type
			if (($this->input->post('type') == 1)){
				$company = array();
				if ($this->input->post('company')){
					$c = $this->input->post('company');
					if ($this->option['restrictMax']){
						$company = $this->CompanyModel->get($c);
						if ($company->regNum >= $company->traineeNum*1.2){
							$this->session->set_userdata('msg','Max number reached');
							$this->session->set_userdata('msgtype','error');
							redirect(site_url('internship'));
						}
					}
					$data = array(
						'userid' => $this->session->userdata('userid'),
						'companyid' => $c
					);
					$this->db->insert('link',$data);
				}
				redirect(site_url('internship'));
			}else{
				$newdata['userid'] = $this->headerdata['userid'];
				$newdata['company'] = $this->input->post('company-custom');
				$this->db->insert('link_other',$newdata);
				redirect(site_url('internship'));
			}
		}else{
			redirect(site_url('internship'));
		}
	}
	
	function company(){
		$data = array();
		$data_1= array();
		$data_2= array();
		$success = '';
		$error = '';
		
		$this->load->model('CompanyModel');
		$this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		// load thư viện cần thiết cho phân trang
        $this->load->library('pagination');
        $this->load->helper('url');
        //phân trang
		$config['base_url'] = site_url('/internship/company/'); // xác định trang phân trang
        $config['total_rows'] = $this->CompanyModel->count_all(); // xác định tổng số record
        $config['per_page'] = 3; // xác định số record ở mỗi trang
        $config['uri_segment'] = 2; // xác định segment chứa page number
        $this->pagination->initialize($config); 

		//bắt post cty có trong danh sách
		if($this->input->post('company') != '') {
			foreach ($this->input->post('company') as $key) {
				$data_1['userid']= $this->session->userdata('userid');
				$data_1['companyid']= $key;
				$test1= $this->CompanyModel->regis_intership($data_1);
				if(!$test1) $error = "Đăng ký không thành công";
				else $success = "Đăng ký thành công";
			}
		}

		//bắt post công ty ko có trong danh sách
		if($this->input->post('company_other_name') != '' && $this->input->post('company_other_email') != '' 
			&& $this->input->post('company_other_address') != '') {
			$data_2['userid']	= $this->session->userdata('userid');
			$data_2['company']	= $this->input->post('company_other_name');
			$data_2['email']	= $this->input->post('company_other_email');
			$data_2['address']	= $this->input->post('company_other_address');
			
			$test= $this->CompanyModel->regis_other($data_2);
			if(!$test) $error = "Đăng ký không thành công";
			else $success = "Đăng ký thành công";
		}

		$filter = array();
		if ($this->input->post('name') != '') $filter['name'] = $this->input->post('name');
		if ($this->input->post('email') != '') $filter['email'] = $this->input->post('email');
		if ($this->input->post('require') != '') $filter['require'] = $this->input->post('require');
		
		$this->headerdata['route2'] = 'Thông tin công ty';
		
		//$data['companies'] = $this->CompanyModel->list_all($config['per_page'],$this->uri->segment(2)); 
		$data['companies'] = $this->CompanyModel->get_pagging(0, $filter, $config['per_page'], $this->uri->segment(2));

		$data['filter'] = $filter;
		$data['link'] = $this->pagination->create_links();
		
		$data['success'] = $success;
		$data['error'] = $error;

		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('company',$data);
		$this->load->view('common/footer');
	}

	public function acceptintership()
	{
		$this->db->where('link.companyid',$this->session->userdata('userid'));
		//$this->db->join('user', 'user.id = link.userid');
		//$this->db->select('user.user,user.cv,user.fullname,link.userid,link.result,user.freetime');
		$q = $this->db->get('link');
		$data['list'] = $q->result();
		//$data['companies'] = $this->CompanyModel->get();

		//$data['id'] = $id;

		$this->load->model('UserModel');
		//$data['user'] = $this->UserModel->get($this->headerdata['userid']);
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		// $data['success'] = $success;
		// $data['error'] = $error;
		$this->headerdata['route2'] = 'Kết quả đăng ký';
		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('accept_intership',$data);
		$this->load->view('common/footer');
	}
}
?>